package com.example.mq.mqserver.datacenter;

import com.example.mq.common.MqException;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.core.Message;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class MemoryDataCenter {
    private ConcurrentHashMap<String, Exchange> exchangeMap=new ConcurrentHashMap<>();
    private ConcurrentHashMap<String, MSGQueue> queueMap=new ConcurrentHashMap<>();
    private ConcurrentHashMap<String,ConcurrentHashMap<String, Binding>> bindingsMap=new ConcurrentHashMap<>();
    private ConcurrentHashMap<String, Message> messageMap=new ConcurrentHashMap<>();
    private ConcurrentHashMap<String, LinkedList<Message>> queueMessageMap=new ConcurrentHashMap<>();
    private ConcurrentHashMap<String,ConcurrentHashMap<String,Message>> queueMessageWaitAckMap=new ConcurrentHashMap<>();

    public void insertExchange(Exchange exchange){
        exchangeMap.put(exchange.getName(),exchange);
        System.out.println("[MemoryDataCenter] 新交换机添加成功！exchangeName="+exchange.getName());
    }
    public Exchange getExchange(String exchangeName){
        return exchangeMap.get(exchangeName);
    }
    public void deleteExchange(String exchangeName){
        exchangeMap.remove(exchangeName);
        System.out.println("[MemoryDataCenter] 交换机删除成功！exchangeName="+exchangeName);
    }

    public void insertQueue(MSGQueue queue){
        queueMap.put(queue.getName(),queue);
        System.out.println("[MemoryDataCenter] 新队列添加成功！queueName="+queue.getName());
    }
    public MSGQueue getQueue(String queueName){
        return queueMap.get(queueName);
    }
    public void deleteQueue(String queueName){
        queueMap.remove(queueName);
        System.out.println("[MemoryDataCenter] 新队列删除成功！queueName="+queueName);
    }

    public void insertBinding(Binding binding) throws MqException {
        ConcurrentHashMap<String,Binding> bindingMap=bindingsMap.computeIfAbsent(binding.getExchangeName(),k->new ConcurrentHashMap<>());
        synchronized (bindingMap){
            if(bindingMap.get(binding.getQueueName())!=null){
                throw new MqException("[MemoryDataCenter] 绑定已经存在！exchange Name="+binding.getExchangeName()+",queueName="+binding.getQueueName());
            }
            bindingMap.put(binding.getQueueName(),binding);
        }
        System.out.println("[MemoryDataCenter] 新绑定添加成功！exchangeName="+binding.getExchangeName()+",queueName="+binding.getQueueName());
    }
    public Binding getBinding(String exchangeName,String queueName){
        ConcurrentHashMap<String,Binding> bindingMap=bindingsMap.get(exchangeName);
        if(bindingMap==null){
            return null;
        }
        return bindingMap.get(queueName);
    }
    public ConcurrentHashMap<String,Binding> getBindings(String exchangeName){
        return bindingsMap.get(exchangeName);
    }
    public void deleteBinding(Binding binding) throws MqException {
        ConcurrentHashMap<String,Binding> bindingMap=bindingsMap.get(binding.getExchangeName());
        if(bindingMap==null){
            throw new MqException("[MemoryDataCenter] 绑定不存在！exchangeName="+binding.getExchangeName()+",queueName="+binding.getQueueName());
        }
        bindingMap.remove(binding.getQueueName());
        System.out.println("[MemoryDataCenter] 绑定删除成功！exchangeName="+binding.getExchangeName()+",queueName="+binding.getQueueName());
    }

    public void addMessage(Message message){
        messageMap.put(message.getMessageId(),message);
        System.out.println("[MemoryDataCenter] 新消息添加成功！messageId="+message.getMessageId());
    }
    public Message getMessage(String messageId){
        return messageMap.get(messageId);
    }
    public void removeMessage(String messageId){
        messageMap.remove(messageId);
        System.out.println("[MemoryDataCenter] 消息删除成功！messageId="+messageId);
    }

    public void sendMessage(MSGQueue queue,Message message){
        LinkedList<Message> messages=queueMessageMap.computeIfAbsent(queue.getName(),k->new LinkedList<>());
        synchronized (messages){
            messages.add(message);
        }
        addMessage(message);
        System.out.println("[MemoryDataCenter] 消息被投递到队列中！messageId="+message.getMessageId());
    }

    public Message pollMessage(String queueName){
        LinkedList<Message> messages=queueMessageMap.get(queueName);
        if(messages==null){
            return null;
        }
        synchronized (messages){
            if(messages.size()==0){
                return null;
            }
            Message currentMessage=messages.remove(0);
            System.out.println("[MemoryDataCenter]消息从队列中取出！MessageId="+currentMessage.getMessageId());
            return currentMessage;
        }
    }

    public int getMessageCount(String queueName){
        LinkedList<Message> messages=queueMessageMap.get(queueName);
        if(messages==null){
            return 0;
        }
        synchronized (messages) {
            return messages.size();
        }
    }

    public void addMessageWaitAck(String queueName,Message message){
        ConcurrentHashMap<String,Message> messageHashMap=queueMessageWaitAckMap.computeIfAbsent(queueName,k->new ConcurrentHashMap<>());
        messageHashMap.put(message.getMessageId(),message);
        System.out.println("[MemoryDataCenter]消息进入待确认队列！MessageId="+message.getMessageId());
    }

    public void removeMessageWaitAck(String queueName,String messageId){
        ConcurrentHashMap<String,Message> messageHashMap=queueMessageWaitAckMap.get(queueName);
        if(messageHashMap==null){
            return;
        }
        messageHashMap.remove(messageId);
        System.out.println("[MemoryDataCenter]消息从待确认队列删除！MessageId="+messageId);
    }

    public Message getMessageWaitAck(String queueName,String messageId){
        ConcurrentHashMap<String,Message> messageHashMap=queueMessageWaitAckMap.get(queueName);
        if(messageHashMap==null){
            return null;
        }
        return messageHashMap.get(messageId);
    }

    public void recovery(DiskDataCenter diskDataCenter) throws IOException, MqException, ClassNotFoundException {
        exchangeMap.clear();
        queueMap.clear();
        bindingsMap.clear();
        messageMap.clear();
        queueMessageMap.clear();

        List<Exchange> exchanges=diskDataCenter.selectAllExchanges();
        for (Exchange exchange:exchanges){
            exchangeMap.put(exchange.getName(),exchange);
        }
        List<MSGQueue> queues=diskDataCenter.selectAllQueues();
        for (MSGQueue queue:queues){
            queueMap.put(queue.getName(),queue);
        }
        List<Binding> bindings=diskDataCenter.selectAllBindings();
        for (Binding binding:bindings){
            ConcurrentHashMap<String,Binding> bindingMap=bindingsMap.computeIfAbsent(binding.getExchangeName(),k->new ConcurrentHashMap<>());
            bindingMap.put(binding.getQueueName(),binding);
        }
        for (MSGQueue queue:queues){
            LinkedList<Message> messages=diskDataCenter.loadAllMessageFromQueue(queue.getName());
            queueMessageMap.put(queue.getName(),messages);
            for (Message message:messages){
                messageMap.put(message.getMessageId(),message);
            }
        }
    }
}
